<?php

namespace app\api\service;
use OSS\OssClient;
use OSS\Core\OssException;
use app\api\exception\Exception;

class Media {

  public function uploadOss($object, $content) {
    try {
      $ossClient = new OssClient(
        config('aliyun.accessKeyId'), 
        config('aliyun.accessKeySecret'),
        config('aliyun.endpoint')
      );
      $res= $ossClient->putObject(
        config('aliyun.bucket'), 
        config('aliyun.folder') . '/' . $object, 
        $content
      );
    } catch (OssException $e) {
      throw new Exception($e->getMessage());
    }
    return $res['info']['url'];
  }

  public function deleteOss($object) {
    try {
      $ossClient = new OssClient(
        config('aliyun.accessKeyId'), 
        config('aliyun.accessKeySecret'),
        config('aliyun.endpoint')
      );
      $ossClient->deleteObject(config('aliyun.bucket'), $object);
    } catch (OssException $e) {
      throw new Exception($e->getMessage());
    }
    return success();
    
  }
  
}